﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using Afiniczny.Properties;

namespace Afiniczny
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private CipherAffineAlgorithm _cipherAffineAlgorithm;

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == ofdOpenFile.ShowDialog())
            {
                rtbText.Text = ReadFile(ofdOpenFile.FileName);
            }
        }

        private string ReadFile(string a_filePath)
        {
            var stream = new StreamReader(a_filePath);
            return stream.ReadToEnd();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            panelManualKeys.Visible = false;
            _cipherAffineAlgorithm = new CipherAffineAlgorithm();
            updateInfoLabels();
        }

        void updateInfoLabels()
        {
            lbKeys.Text = Resources.Form1_UpdateInfoLabels_A__ + _cipherAffineAlgorithm.GetKeyA() + Resources.Form1_UpdateInfoLabels__B__ + _cipherAffineAlgorithm.GetKeyB();
        }

        private void btnCode_Click(object sender, EventArgs e)
        {
            if(rtbText.Text == "")
                return;

            if (!isTextValid())
            {
                MessageBox.Show(Resources.InvalidText);
                return;
            }

            rtbText.Text = _cipherAffineAlgorithm.EncryptText(rtbText.Text);
        }

        private void btnDecode_Click(object sender, EventArgs e)
        {
            if (rtbText.Text != "")
            {
                rtbText.Text = _cipherAffineAlgorithm.DecryptText(rtbText.Text);
            }
        }

        private void manualnieToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelManualKeys.Location = new Point((Size.Width/2 - panelManualKeys.Width/2),
                                                 (Size.Height/2 - panelManualKeys.Height));
 
            panelManualKeys.Visible = true;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            panelManualKeys.Location = new Point((Size.Width / 2 - panelManualKeys.Width/2),
                                                 (Size.Height / 2 - panelManualKeys.Height));
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (isFilledKeyTextBoxes())
            {
                if (_cipherAffineAlgorithm.CheckKeyConditions(int.Parse(tbKeyA.Text)))
                {
                    if (int.Parse(tbKeyB.Text) < Alphabet.getAlphabetLenght())
                    {
                        _cipherAffineAlgorithm = new CipherAffineAlgorithm(int.Parse(tbKeyA.Text), int.Parse(tbKeyB.Text));
                        panelManualKeys.Visible = false;
                        updateInfoLabels();
                    }
                    else
                    {
                        MessageBox.Show(Resources.Form1_InvalidKeyB);
                    }
                }
                else
                {
                    MessageBox.Show(Resources.Form1_InvalidKeyA);
                }
            }
            else
            {
                MessageBox.Show(Resources.Form1_WriteKeys);
            }
        }

        private void automatycznieToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (panelManualKeys.Visible)
                panelManualKeys.Visible = false;

            _cipherAffineAlgorithm = new CipherAffineAlgorithm();
            updateInfoLabels();
        }

        //Validatory

        bool isFilledKeyTextBoxes()
        {
            if (tbKeyA.Text == "" || tbKeyB.Text == "")
                return false;
            return true;
        }

        bool isTextValid()
        {
            foreach (var character in rtbText.Text)
            {
                if (!Alphabet.isContainCharacter(character))
                    return false;
            }
            return true;
        }

        private void btnPanelClose_Click(object sender, EventArgs e)
        {
            panelManualKeys.Visible = false;
        }
    }
}
